package com.gcsj.gcsjspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.gcsj.gcsjspringboot.common.annotation.SysLog;
import com.gcsj.gcsjspringboot.common.utils.DateUtils;
import com.gcsj.gcsjspringboot.common.utils.PageUtils;
import com.gcsj.gcsjspringboot.common.utils.R;
import com.gcsj.gcsjspringboot.common.validator.ValidatorUtils;
import com.gcsj.gcsjspringboot.entity.SalaryRecordEntity;
import com.gcsj.gcsjspringboot.service.SalaryRecordService;
import com.gcsj.gcsjspringboot.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 工资
 *
 * @author
 */
@RestController
@RequestMapping("/salary")
public class SalaryRecordController extends AbstractController {
    @Autowired
    private SalaryRecordService salaryRecordService;
    @Autowired
    SysUserService userService;
    /**
     * 所有工资列表
     */
    @RequestMapping("/list")
    public R list(@RequestBody Map<String, Object> params) {
        PageUtils page = salaryRecordService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 生成工资表
     */
    @RequestMapping("/genST")
    public R genST(String date) {
        int i = salaryRecordService.genSalaryT(date);
        if(i == -1){
            return R.ok("这个月的工资表不可重复生成");
        }
        return R.ok();
    }

    /**
     * 保存工资
     */
    @RequestMapping("/save")
    public R save(@RequestBody SalaryRecordEntity salary) {
        ValidatorUtils.validateEntity(salary);
        salary.setCreateTime(DateUtils.currentTimeFormat());
        //查询当日记录是否已存在
        SalaryRecordEntity d = new SalaryRecordEntity();
        String salaryMonth = salary.getSalaryMonth();
        d.setUserId(salary.getUserId());
        d.setSalaryMonth(salaryMonth);
        QueryWrapper q = new QueryWrapper();
        q.setEntity(d);
        SalaryRecordEntity one = salaryRecordService.getOne(q);
        if (null != one) {
            return R.error(salaryMonth + "月工资已核算");
        }
        salaryRecordService.save(salary);
        return R.ok();
    }

    /**
     * 修改工资
     */
    @RequestMapping("/update")
    public R update(@RequestBody SalaryRecordEntity salary) {
        boolean b = salaryRecordService.updateById(salary);
        if(b){
            return R.ok();
        }
        return R.error("出错了");
    }

    /**
     * 删除工资
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        salaryRecordService.deleteBatch(ids);
        return R.ok();
    }

}
